package com.intel.factorytest.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.hardware.display.DisplayManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.net.Uri;
import android.content.ContentResolver;
import android.graphics.PixelFormat;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View.OnClickListener;

import com.intel.factorytest.R;

public class HDMITest extends BaseActivity implements OnClickListener,
        SurfaceHolder.Callback,OnCompletionListener{

    public static final String TAG = "HDMITest";
    private DisplayManager mDisplayManager = null;
    private TextView tv = null;
    Button mSucBtn;
    Button mPlayBtn;
    boolean isPlaying = false;

    SurfaceHolder surfaceHolder;
    private MediaPlayer mediaPlayer;
    SurfaceView surfaceView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Get the display manager service.
        mDisplayManager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);

        setContentView(R.layout.hdmi_layout);
        mSucBtn = (Button) findViewById(R.id.success_button);
        mSucBtn.setVisibility(View.INVISIBLE);
        mPlayBtn = (Button) findViewById(R.id.playButton);
        initView();
        updateContents();
        
    }

    protected void initView() {
        super.initView();
        tv = (TextView) findViewById(R.id.hdmiTextView);
        surfaceView = (SurfaceView) findViewById(R.id.videoView);
        
        getWindow().setFormat(PixelFormat.UNKNOWN);
        mediaPlayer = new MediaPlayer();
    }
    
    private Uri getMediaVolumeUri(Context context) {
        System.out.println("tangjun video: uri=" + Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
                + context.getPackageName() + "/" + R.raw.video).toString());
        return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
                + context.getPackageName() + "/" + R.raw.video);
    }

    private void updateContents() {
        Display[] displays = mDisplayManager
                .getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION);
        String textString = getString(R.string.SERVICEMENU_HDMI_UNAVAILABLE_HDMI_TXT);

        for (Display display : displays) {
            if (Display.TYPE_EXTERNAL == display.getType()) {
                textString = getString(R.string.SERVICEMENU_HDMI_FOUND_HDMI_TXT);
                //mPlayBtn.setVisibility(View.VISIBLE);
                mPlayBtn.setText(R.string.SERVICEMENU_VIDEO_PLAY_TXT);
                mPlayBtn.setOnClickListener(this);
                //mSucBtn.setVisibility(View.VISIBLE);
                break;
            }
        }
        tv.setText(textString);

    }

    private final DisplayManager.DisplayListener mDisplayListener = new DisplayManager.DisplayListener() {
        @Override
        public void onDisplayAdded(int displayId) {
            Log.d(TAG, "Display #" + displayId + " added.");
            updateContents();
        }

        @Override
        public void onDisplayChanged(int displayId) {
            Log.d(TAG, "Display #" + displayId + " changed.");
            updateContents();
        }

        @Override
        public void onDisplayRemoved(int displayId) {
            Log.d(TAG, "Display #" + displayId + " removed.");
            updateContents();
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        // Register to receive events from the display manager.
        mDisplayManager.registerDisplayListener(mDisplayListener, null);
    }

    @Override
    protected void onPause() {
        super.onPause();
        // Unregister from the display manager.
        mDisplayManager.unregisterDisplayListener(mDisplayListener);
    }

    @Override
    protected void onDestroy() {
        Log.i(TAG, "onDestroy()");
        if (mediaPlayer != null)
            mediaPlayer.release();
        mediaPlayer= null;
        super.onDestroy();

    }

    @Override
    public void finish() {
      stop_mp();
        tc.setTestResult(is_pass);
        super.finish();
    }
    
    public void onClick(View v) {
        if (v == mPlayBtn) {
          if (mediaPlayer != null) {
                    if (!mediaPlayer.isPlaying()) {
                      tv.setVisibility(View.INVISIBLE);
                      surfaceView.setVisibility(View.VISIBLE);
                      surfaceHolder = surfaceView.getHolder();
                      surfaceHolder.addCallback(this);
                      surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
                      start_mp();
                      mPlayBtn.setVisibility(View.INVISIBLE);
                      mSucBtn.setVisibility(View.VISIBLE);
                      isPlaying = true;
                        return;
                    } else {
                      mediaPlayer.pause();
                      isPlaying = false;
                    }
                } else {
                    return;
                }
        }
    }
    
    private void stop_mp() {
        Log.i(TAG, "stop_mp()");
        if (mediaPlayer!=null)
        {
            if (mediaPlayer.isPlaying())
                mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }
    
    private void start_mp() {
        try {
            Log.i(TAG, "start_mp()");
            mediaPlayer.setDisplay(surfaceHolder);
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayer.setOnCompletionListener(this);
            Log.d("chenwei", "mediaplayer is start...");
            mediaPlayer.setDataSource(HDMITest.this, getMediaVolumeUri(this));
            mediaPlayer.prepare();
            mediaPlayer.start();
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        Log.i(TAG, "surfaceChanged");

    }

    public void surfaceCreated(SurfaceHolder holder) {
        Log.i(TAG, "surfaceCreated");
        surfaceHolder = holder;
        /*
        if (mediaPlayer != null) {
            try {
                mediaPlayer.setDisplay(surfaceHolder);
                mediaPlayer.setDataSource(this, getMediaVolumeUri(this));
                mediaPlayer.prepare();
                mediaPlayer.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        */
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.i(TAG, "surfaceDestroyed");
        holder.removeCallback(this);  
    }
    
    @Override
    public void onCompletion(MediaPlayer mp) {
        // TODO Auto-generated method stub
        Log.i(TAG, "onCompletion()");
        stop_mp();
    }

}
